package com.llq.report;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import com.llq.studentinfo.Session;
import com.llq.studentinfo.Student;
import com.llq.util.LineUtil;

public class RosterRoprter {

	// public static final String NEWLINE =
	// System.getProperty("line.separator");
	public static final String ROSTER_REPORT_HEADER = "Student%n-%n"
			+ LineUtil.NEWLINE;
	public static final String ROSTER_REPORT_FOOTER = "%n# students = %d%n"
			+ LineUtil.NEWLINE;
	private Session session;
	private Writer writer;

	public RosterRoprter(Session session) {
		this.session = session;
	}

	// public String getReport() {
	// StringBuilder builder=new StringBuilder();
	// writeHeader(builder);
	// writeBody(builder);
	// writeFooter(builder);
	//		
	// return builder.toString();
	// }
	//
	// private void writeFooter(StringBuilder builder) {
	// builder.append(ROSTER_REPORT_FOOTER);
	// builder.append(courseSession.getAllStudents().size());
	// builder.append(LineUtil.NEWLINE);
	// }
	//
	// private void writeBody(StringBuilder builder) {
	// // List<Student> allStudents=courseSession.getAllStudents();
	// for (Student student : courseSession.getAllStudents()) {
	// builder.append(student.getName());
	// builder.append(LineUtil.NEWLINE);
	// }
	// }
	//
	// private void writeHeader(StringBuilder builder) {
	// builder.append(ROSTER_REPORT_HEADER);
	// }
	//
	public void writeReport(Writer writer) throws IOException {
		this.writer = writer;
		writeHeader();
		writeBody();
		writeFooter();
	}
	public void writeReport(String filename) throws IOException{
		Writer writer=new BufferedWriter(new FileWriter(filename));
		try {
			writeReport(writer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			writer.close();
		}
	
		
	}

	private void writeFooter() throws IOException {
		writer.write(String.format(ROSTER_REPORT_FOOTER,session.getAllStudents().size()));
	}

	private void writeBody() throws IOException {
		for (Student student : session.getAllStudents()) {
			writer.write(String.format(student.getName()+"%n"));
		}
	}

	private void writeHeader() throws IOException {
		writer.write(String.format(ROSTER_REPORT_HEADER));
	}

}
